package com.vimi8.chick.plugins.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import android.widget.Toast
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import java.io.File

object ShareUtil {

    fun shareText(activity: Activity, text: String, image: String?, callback: UMShareListener? = UMShareListenerImpl(activity)) {
        ShareAction(activity)
            .withText(text)
            .withMedia(UMImage(activity, image))
            .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
            .setCallback(callback)
            .open()
    }

    fun shareUrl(activity: Activity, title: String, url: String?, description: String?, image: String?, callback: UMShareListener? = UMShareListenerImpl(activity)) {
        val web = UMWeb(url)
        web.title = title
        web.description = description
        if (!image.isNullOrEmpty()) {web.setThumb(UMImage(activity, image))}
        ShareAction(activity)
            .withMedia(web)
            .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
            .setCallback(callback)
            .open()
    }

    open class UMShareListenerImpl(var activity: Activity) : UMShareListener {
        override fun onResult(p0: SHARE_MEDIA?) {
            Toast.makeText(activity, "分享成功", Toast.LENGTH_SHORT).show()
        }

        override fun onCancel(p0: SHARE_MEDIA?) {
            Toast.makeText(activity, "你取消了分享!", Toast.LENGTH_SHORT).show()
        }

        override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
            val mShareAPI = UMShareAPI.get(activity)
            val msg = when {
                (p0 == SHARE_MEDIA.WEIXIN && !mShareAPI.isInstall(activity, SHARE_MEDIA.WEIXIN)) or
                    (p0 == SHARE_MEDIA.WEIXIN_CIRCLE && !mShareAPI.isInstall(activity, SHARE_MEDIA.WEIXIN_CIRCLE))
                -> {
                    "您的手机还没有安装微信"
                }
                (p0 == SHARE_MEDIA.QQ && !mShareAPI.isInstall(activity, SHARE_MEDIA.QQ)) or
                    (p0 == SHARE_MEDIA.QZONE && !mShareAPI.isInstall(activity, SHARE_MEDIA.QZONE))
                -> {
                    "您的手机还没有安装QQ"
                }
                else -> {
                    "分享失败!"
                }
            }
            Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show()
        }

        override fun onStart(p0: SHARE_MEDIA?) {

        }
    }

    /**
     * 原生方法分享本地文件
     */
    fun shareLocalFile(activity: Context, file: File, title: String) {
        val intent = Intent(Intent.ACTION_SEND)
        intent.putExtra(Intent.EXTRA_STREAM, getUriFromFile(activity, file))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        intent.type = FileUtil.getMimeType(file.absolutePath)
        intent.putExtra("Kdescription", title)
        activity.startActivity(Intent.createChooser(intent, title))
    }

    /**
     * 原生方法分享多个本地文件
     */
    fun shareLocalFile(activity: Context, files: MutableList<File>, title: String) {

        var fileType = "*/*"
        val uris = arrayListOf<Uri>()
        for (file in files) {
            uris.add(getUriFromFile(activity, file))
        }
        if (files.size == 1) {
            fileType = FileUtil.getMimeType(files[0].absolutePath)
        }

        val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
        intent.type = fileType
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        intent.putExtra("Kdescription", title)
        activity.startActivity(Intent.createChooser(intent, title))
    }

    private fun getUriFromFile(context: Context, file: File): Uri {
        return when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> FileProvider.getUriForFile(
                context,
                "${context.packageManager.getPackageInfo(context.packageName, 0).packageName}.fileProvider",
                file
            )
            else -> Uri.fromFile(file)
        }
    }
}
